<!--
 * @Description:
 * @Autor: 刘诗文
 * @Date: 2020-11-30 10:52:40
 * @LastEditors: 刘诗文
 * @LastEditTime: 2020-12-03 20:11:55
-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="105" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      height="500"
      selectionType="multiple"
      :columns="columns"
      :datakey="'rows'"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :loading="loading"
      columnsRef="returnVisitSettingslsw"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="onRowSelectChange"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="() => this.openHandler('add', {})">新增</el-button>
        <AjaxButton size="small" type="primary" :click="release">发布</AjaxButton>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="actions.visible" :title="actions.title" width="300" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addSettingsModel :type.sync="actions.type" :formData="this.resData" @close="closeSetting" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import addSettingsModel from './addSettingsModel';
import { findAllSettings, deleteSettings, sendSettings, stopSettings } from '@/api/customerRepair/visitRules/returnVisitSettings';

export default {
  name: 'returnVisitSettings',
  components: {
    addSettingsModel
  },
  mixins: [dictionary],
  data() {
    this.selectedData = null;
    this.BaseTable = null;
    this.resData = {};
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      actions: { type: '', visible: false, title: '' },
      fetchApi: findAllSettings,
      fetchParams: {},
      loading: false
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '计划名称',
          fieldName: 'settingName',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'status',
          itemList: this.createDictList(1004)
        },
        {
          type: 'RANGE_DATE',
          label: '创建时间',
          fieldName: 'createdAtFrom|createdAtFromTo'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.openHandler('delete', props.row)}>
                  删除
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.openHandler('stop', props.row)}>
                  暂停
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={props.row.sendStatus === 12891001 ? true : false} onClick={() => this.openHandler('edit', props.row)}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" disabled={props.row.sendStatus === 12891002 ? true : false} onClick={() => this.openHandler('detail', props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '计划编号',
          dataIndex: 'settingNo',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '回访类型',
          dataIndex: 'type',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.createDictText(props.row.type, 1263)}</span>;
          }
        },
        {
          title: '回访名称',
          dataIndex: 'settingName',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          width: 120,
          showOverFlowTip: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '基准日期',
          dataIndex: 'baseDateType',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{'出库日期'}</span>;
          }
        },
        {
          title: '基准天数',
          dataIndex: 'baseDate',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '基准前后',
          dataIndex: 'a7',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '分配方式',
          dataIndex: 'a8',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '分配人员',
          dataIndex: 'rule',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.createDictText(props.row.rule, 1262)}</span>;
          }
        },
        {
          title: '是否启用',
          dataIndex: 'status',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.createDictText(props.row.status, 1004)}</span>;
          }
        },
        {
          title: '执行周期',
          dataIndex: 'a11',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '发布状态',
          dataIndex: 'sendStatus',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.createDictText(props.row.sendStatus, 1289)}</span>;
          }
        }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(this.BaseTable.EXECUTE_RESET_HEIGHT);
    },
    onRowSelectChange(row) {
      if (!row.length) {
        this.selectedData = null;
      } else {
        this.selectedData = row;
      }
    },
    // 发布
    async release() {
      this.$refs.table.START_LOADING();
      const settingIds = [];
      this.selectedData.forEach(x => {
        settingIds.push(x.settingId);
      });
      console.log(settingIds);
      const res = await sendSettings(settingIds);
      if (res.resultCode === 200) {
        if (res.errMsg) {
          notifyAction(res.errMsg, 'error');
        } else {
          notifyAction('发布成功');
          this.selectedData = null;
          this.fetchParams = { ...this.fetchParams };
        }
      }
      this.$refs.table.STOP_LOADING();
    },
    openHandler(type, val) {
      if (type === 'add') {
        this.actions = { visible: true, type: 'add', title: '新增回访计划' };
      } else if (type === 'delete') {
        this.delete(val);
      } else if (type === 'stop') {
        this.stop(val);
      } else if (type === 'edit') {
        this.actions = { visible: true, type: 'edit', title: '编辑回访计划' };
        this.resData = val;
      } else if (type === 'detail') {
        this.actions = { visible: true, type: 'detail', title: '回访计划明细' };
        this.resData = val;
      }
    },
    // 删除
    async delete(row) {
      this.$refs.table.START_LOADING();
      try {
        if (row.sendStatus === 12891002) {
          return notifyAction('已发布的回访设置不能删除！', 'warning');
        } else if (row.sendStatus === 12891001) {
          const res = await deleteSettings(row.settingId);
          if (res.resultCode === 200) {
            if (res.errMsg) {
              notifyAction(res.errMsg, 'error');
            } else {
              notifyAction('删除成功');
              this.fetchParams = { ...this.fetchParams };
            }
          }
        } else {
          return notifyAction('无发布状态，请联系管理员！', 'error');
        }
      } catch (err) {
        console.log(err, '查询table主数据失败');
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    // 停用
    async stop(row) {
      this.$refs.table.START_LOADING();
      try {
        if (row.status === 10041001) {
          const res = await stopSettings(row.settingId);
          if (res.resultCode === 200) {
            if (res.errMsg) {
              notifyAction(res.errMsg, 'error');
            } else {
              notifyAction('停用成功');
              this.fetchParams = { ...this.fetchParams };
            }
          }
        } else if (row.status === 10041002) {
          return notifyAction('已停用的回访设置不能停用！', 'warning');
        } else {
          return notifyAction('无启用状态，请联系管理员！', 'error');
        }
      } catch (err) {
        console.log(err, '查询table主数据失败');
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    closeSetting() {
      this.actions.visible = false;
      this.fetchParams = { ...this.fetchParams };
    }
  }
};
</script>
<style lang="less" scoped></style>
